package com.agent.mapper;

import com.agent.entity.Activities;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;

@Mapper
public interface ActivitiesMapper extends BaseMapper<Activities> {

    @Insert("INSERT INTO activities (user_id, activity, start_time, end_time, all_day, week, date, created_at, updated_at) " +
            "VALUES (#{userId}, #{activity}, #{startTime}, #{endTime}, #{allDay}, #{week}, #{date}, #{createdAt}, #{updatedAt})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertActivity(Activities activity);

    // 查询指定日期的活动安排
//    @Select("SELECT * FROM activities WHERE user_id = #{userId} AND date = #{date}")
    List<Activities> findByUserIdAndDate(@Param("userId") Long userId, @Param("date") LocalDate date, @Param("startTime") LocalTime startTime, @Param("endTime") LocalTime endTime);

    @Select("SELECT * FROM activities WHERE user_id = #{userId} AND week = #{week}")
    List<Activities> findByUserIdAndWeek(@Param("userId") Long userId, @Param("week") Integer week);

    @Select("SELECT * FROM activities WHERE user_id = #{userId} AND date = #{date} " +
            "AND ((start_time <= #{endTime} AND end_time >= #{startTime}) OR all_day = 1)")
    List<Activities> findConflictingActivities(@Param("userId") Long userId,
                                             @Param("date") LocalDate date,
                                             @Param("startTime") LocalTime startTime,
                                             @Param("endTime") LocalTime endTime);

    @Update("UPDATE activities SET activity = #{activity}, start_time = #{startTime}, " +
            "end_time = #{endTime}, all_day = #{allDay}, updated_at = #{updatedAt} WHERE id = #{id}")
    int updateActivity(Activities activity);

    @Delete("DELETE FROM activities WHERE id = #{id} AND user_id = #{userId}")
    int deleteActivity(@Param("id") Long id, @Param("userId") Long userId);
}
